// 是否是字符串
export function isEmptyStr(str) {
  return (
    str === undefined ||
    (!str && str !== 0 && str !== "0") ||
    !/[^\s]/.test(str)
  );
}

// 将文本按照回车分割成数组
export function splitTextByEnter(text) {
  return text.split(/\r|\n/);
}

/**
 * 防抖函数
 * @param fn 需要防抖的函数
 * @param delay 防抖的时间
 */
export function debounce(fn, delay) {
  let timer = null;
  return function () {
    let context = this;
    let args = arguments;
    clearTimeout(timer);
    timer = setTimeout(function () {
      fn.apply(context, args);
    }, delay);
  };
}

// 获取路由参数
export function getRouteParams(): { [key: string]: string } {
  const params = {};
  const search = window.location.search;
  if (search) {
    const searchArr = search.slice(1).split("&");
    searchArr.forEach((item) => {
      const [key, value] = item.split("=");
      params[key] = value;
    });
  }
  return params;
}
